package com.javastudy.thread;

public class ThreadState {
//    public static Thread thread1;
//    public static ThreadState obj;

    public static void main(String[] args) throws InterruptedException {
        // 第一种状态切换： 新建 -> 运行 -> 终止
        System.out.println("######第一种状态切换： 新建 -> 运行 -> 终止######");
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread1当前状态" + Thread.currentThread().getState().toString());
                System.out.println("thread1执行了");
            }
        });
        System.out.println("没有调用start方法，thread1当前状态：" + thread1.getState().toString());
        thread1.start();
        Thread.sleep(2000L);// 等待thread1执行结束，再看状态
        System.out.println("等待两秒，再看thread1当前状态：" + thread1.getState().toString());
//        thread1.start();

        System.out.println();
        System.out.println("######第二种状态切换： 新建 -> 运行 -> 等待 -> 运行 -> 终止######");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread2当前状态1：" + Thread.currentThread().getState().toString());//RUNNABLE
                try {//将线程2移动到等待状态，1500后自动唤醒
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread2当前状态2：" + Thread.currentThread().getState().toString());//RUNNABLE
                System.out.println("thread2执行了");
            }
        });
        System.out.println("每调用start方法，thread2当前状态：" + thread2.getState().toString());//NEW
        thread2.start();
        System.out.println("调用start方法，thread2当前状态：" + thread2.getState().toString());//RUNNABLE
        Thread.sleep(200);//等待200毫秒
        System.out.println("等待200毫秒，再看thread2状态：" + thread2.getState().toString());//TIMED_WAITING
        Thread.sleep(3000);//等待3秒
        System.out.println("等待3秒，再看thread2状态：" + thread2.getState().toString());//TERMINATED

        System.out.println();
        System.out.println("######第三种状态切换： 新建 -> 运行 -> 阻塞 -> 运行 -> 终止######");
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(ThreadState.class) {
                    System.out.println("thread3当前状态：" + Thread.currentThread().getState().toString());//RUNNABLE
                    System.out.println("thread3执行了");
                }
            }
        });
        synchronized(ThreadState.class){
            System.out.println("没调用start方法，thread3当前状态：" + thread3.getState().toString());//NEW
            thread3.start();
            System.out.println("调用start方法，thread3当前状态：" + thread3.getState().toString());//RUNNABLE
            Thread.sleep(200);
            System.out.println("等待200毫秒，再看thread3状态：" + thread3.getState().toString());//BLOCKED
        }
        Thread.sleep(3000);//再等待3秒，让thread3抢到锁，再看thread3状态
        System.out.println("等待3秒，让thread3抢到锁，再看thread3状态");//terminated
    }
}
